package com.iflytek.edu.db.core.parse;

import com.alibaba.druid.DbType;
import com.iflytek.edu.db.core.enums.AdapterType;
import com.iflytek.edu.db.core.strategy.FunctionStrategy;
import com.iflytek.edu.db.core.strategy.GrammarStrategy;
import com.iflytek.edu.db.core.strategy.Strategy;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author linkai
 * @date 2022/2/10
 */
@Data
public class SqlAdapterConfig {
    private DbType dbType;
    private AdapterType adapterType;
    private List<FunctionStrategy> funcStrategyList;
    private List<GrammarStrategy> grammarStrategyList;

    public void addStrategy(List<Strategy> strategyList) {
        Optional.ofNullable(strategyList).ifPresent(v -> v.forEach(this::addStrategy));
    }

    public void addStrategy(Strategy strategy) {
        if (strategy instanceof FunctionStrategy) {
            addFuncStrategy((FunctionStrategy) strategy);
        } else if (strategy instanceof GrammarStrategy) {
            addGrammarStrategy((GrammarStrategy) strategy);
        }
    }

    public void addFuncStrategy(FunctionStrategy functionStrategy) {
        if (this.funcStrategyList == null) {
            this.funcStrategyList = new ArrayList<>();
        }
        this.funcStrategyList.add(functionStrategy);
    }

    public void addGrammarStrategy(GrammarStrategy grammarStrategy) {
        if (this.grammarStrategyList == null) {
            this.grammarStrategyList = new ArrayList<>();
        }
        this.grammarStrategyList.add(grammarStrategy);
    }
}
